//Prim算法适合边稠密图，时间复杂度O(n²)
typedef pair<int,int> PII;
const int N = 510;
int g[N][N];
int dist[N];
bool st[N];
map<int,int>heap;
int n,m;
int res;
int prim()
{
	memset(dist,0x3f,sizeof dist);
    dist[1] = 0;
	for (int i = 0; i < n; ++i)
	{
		int t = -1;
		for(int j = 1; j <= n; j++)
		{
			if(!st[j]&&(t==-1||dist[j]<dist[t]))
			{
				t = j;
			}
		}

		if(dist[t]==0x3f3f3f3f)
		{
			return 0x3f3f3f3f;
		}

		res += dist[t];

		st[t] = true;

		for(int j = 1; j <= n; j++)
		{
			dist[j] = min(dist[j],g[t][j]);
		}
	}
	return res;
}

void solve()
{ 
	cin >> n >> m;
	memset(g,0x3f,sizeof g);
	for(int i = 1; i <= m; i++)
	{
		int a,b,c;
		cin >> a >> b >> c;
		g[a][b] = g[b][a] = min(g[a][b],c);
	}
	int t = prim();
	if(t == 0x3f3f3f3f) puts("impossible");
    else cout << t << endl;
}
